package com.sk.sqd;

import java.util.HashMap;
import java.util.Map;

/**
 * 享元工厂
 */
public class FlyweightFactory {

    private Map<String, Flyweight> maps = new HashMap<>();

    /**
     * 单例
     */
    private static FlyweightFactory instance = new FlyweightFactory();

    public static FlyweightFactory getInstance() {
        return instance;
    }

    /**
     * 根据 key 获取享元对象
     */
    public Flyweight getFlyweight(String key) {
        // 先从缓存中获取对象，对象不存在则创建对象放入容器中
        if (!maps.containsKey(key)) {
            maps.put(key, new ConcreteFlyweight());
        }
        return maps.get(key);
    }

}
